package com.zero.insp.controller;

import com.zero.insp.common.PJson;
import com.zero.insp.views.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Api(value="用户登录统一请求接口")
@RestController
public class WebLoginController {

    private static final Logger log = LoggerFactory.getLogger(WebLoginController.class);

    @ApiOperation("用户登录接口")
    @GetMapping("/web/v1/login")
    public PJson login(@RequestParam(name= "uname",required = true)String uname, @RequestParam(name= "pwd",required = true)String pwd,
                       @RequestParam(name= "remember",defaultValue = "true")boolean remember){

        String oper = "user login";

        if (StringUtils.isEmpty(uname)){
            return PJson.fail(oper,"用户名不能为空");
        }
        if (StringUtils.isEmpty(pwd)){
            return PJson.fail(oper,"密码不能为空");
        }

        Subject currentUser = SecurityUtils.getSubject();
        try {
            //登录
            currentUser.login( new UsernamePasswordToken(uname, pwd, remember) );
            //从session取出用户信息
            User user = (User) currentUser.getPrincipal();
            if (user==null) throw new AuthenticationException();
            log.info("currentUser session = " + currentUser.getSession().getId());
            //返回登录用户的信息给前台，含用户的所有角色和权限
            return PJson.succ(oper)
                    .data("uid",user.getUid())
                    .data("nick",user.getNick())
                    .data("roles",user.getRoles())
                    .data("perms",user.getPerms())
                    .data("token",currentUser.getSession().getId());

        } catch ( UnknownAccountException uae ) {
            log.warn("用户帐号不正确");
            return PJson.fail(oper,"用户帐号或密码不正确");

        } catch ( IncorrectCredentialsException ice ) {
            log.warn("用户密码不正确");
            return PJson.fail(oper,"用户帐号或密码不正确");

        } catch ( LockedAccountException lae ) {
            log.warn("用户帐号被锁定");
            return PJson.fail(oper,"用户帐号被锁定不可用");

        } catch ( AuthenticationException ae ) {
            log.warn("登录出错");
            return PJson.fail(oper,"登录失败："+ae.getMessage());
        }

    }
    @ApiOperation("用户登录退出接口")
    @RequiresAuthentication
    @GetMapping("/web/v1/logout")
    public PJson logout() {

        Subject currentUser = SecurityUtils.getSubject();
        currentUser.logout();

        return PJson.succ().msg("退出成功");
    }
}
